/*
 * Copyright (c) 2015 www.smarthome.com All rights reserved. 本软件源代码版权归----所有,未经许可不得任意复制与传播.
 */
package com.smarthome.base.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.dao.ShResidenceDao;
import com.smarthome.base.dao.base.BaseDao;
import com.smarthome.base.service.ShResidenceService;
import com.smarthome.base.service.base.BaseServiceImpl;
import com.smarthome.domain.bean.ShResidence;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * ShResidenceService 实现类
 * @author KongXiangyu
 * @since 2015-09-07
 */
@Service("shResidenceService")
public class ShResidenceServiceImpl extends BaseServiceImpl<ShResidence, Long> implements ShResidenceService {

    @Resource
    private ShResidenceDao shResidenceDao;

    public BaseDao<ShResidence, Long> getDao() {
        return shResidenceDao;
    }

    @Override
    public DataGrid queryDataGrid(ShResidence residence, PageHelper page) {
        DataGrid grid = new DataGrid();
        if (null == residence)
            residence = new ShResidence();

        if (!StringUtils.isBlank(page.getOrder())) {
            residence.setOrderFieldType(page.getOrder());
        }
        if (!StringUtils.isBlank(page.getSort())) {
            residence.setOrderField(page.getSort());
        }

        int startIndex = (page.getPage() - 1) * page.getRows();
        int endIndex = page.getPage() * page.getRows();
        Integer count = shResidenceDao.selectEntryListCount(residence);
        grid.setTotal(count + 0l);

        if (grid.getTotal() > 0 && grid.getTotal() > startIndex) {
            residence.setStartIndex(startIndex);
            residence.setEndIndex(endIndex);
            List<ShResidence> rows = shResidenceDao.queryShResidences(residence);
            grid.setRows(rows);
        }

        return grid;
    }

    @Override
    public List<ShResidence> queryResidencesByUserId(Long userId) {
        return shResidenceDao.queryResidencesByUserId(userId);
    }

    @Override
    public List<ShResidence> queryResidencesByAccountId(Long accountId) {
        return shResidenceDao.queryResidencesByAccountId(accountId);
    }
}